package com.yk.course.service.impl;

import java.util.List;
import com.yk.common.utils.DateUtils;
import com.yk.common.utils.ShiroUtils;
import com.yk.course.domain.CourseInfo;
import com.yk.course.domain.CourseInfoChild;
import com.yk.course.domain.CoursePeriod;
import com.yk.course.domain.CoursePeriodChild;
import com.yk.course.mapper.CourseInfoMapper;
import com.yk.course.mapper.CoursePeriodChildMapper;
import com.yk.course.mapper.CoursePeriodMapper;
import com.yk.points.domain.PointsAmount;
import com.yk.points.domain.PointsUserHistory;
import com.yk.points.mapper.PointsAmountMapper;
import com.yk.points.mapper.PointsUserHistoryMapper;
import com.yk.points.mapper.PointsUserMapper;
import com.yk.points.service.impl.PointsUserHistoryServiceImpl;
import com.yk.system.mapper.SysChildMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yk.course.mapper.CourseInfoChildMapper;
import com.yk.course.service.ICourseInfoChildService;
import com.yk.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程学生Service业务层处理
 * 
 * @author xufei
 * @date 2025-04-28
 */
@Service
public class CourseInfoChildServiceImpl implements ICourseInfoChildService 
{
    @Autowired
    private CourseInfoChildMapper courseInfoChildMapper;
    @Autowired
    private CoursePeriodMapper coursePeriodMapper;
    @Autowired
    private CoursePeriodChildMapper coursePeriodChildMapper;
    @Autowired
    private CourseInfoMapper courseInfoMapper;
    @Autowired
    private PointsAmountMapper pointsAmountMapper;
    @Autowired
    private SysChildMapper sysChildMapper;
    @Autowired
    private PointsUserHistoryServiceImpl pointsUserHistoryService;

    /**
     * 查询课程学生
     * 
     * @param id 课程学生主键
     * @return 课程学生
     */
    @Override
    public CourseInfoChild selectCourseInfoChildById(Long id)
    {
        return courseInfoChildMapper.selectCourseInfoChildById(id);
    }

    /**
     * 查询课程学生列表
     * 
     * @param courseInfoChild 课程学生
     * @return 课程学生
     */
    @Override
    public List<CourseInfoChild> selectCourseInfoChildList(CourseInfoChild courseInfoChild)
    {
        return courseInfoChildMapper.selectCourseInfoChildList(courseInfoChild);
    }

    /**
     * 新增课程学生
     * 
     * @param courseInfoChild 课程学生
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCourseInfoChild(CourseInfoChild courseInfoChild) throws Exception {
        Long infoId = courseInfoChild.getInfoId();
        Long childId = courseInfoChild.getChildId();
        Long parentId = courseInfoChild.getParentId();
        courseInfoChild.setDelFlag("0");
        List<CourseInfoChild> list = courseInfoChildMapper.selectList(courseInfoChild);
        if (!list.isEmpty()) {
            throw new Exception("该学生已购买过此课程！");
        }
        //添加积分
        CourseInfo courseInfo = courseInfoMapper.selectCourseInfoById(infoId);
        int pri = courseInfo.getPrice();
        PointsAmount pointsAmount =pointsAmountMapper.selectPointsAmountList(new PointsAmount()).get(0);
        int points = pointsAmount.getPoints();
        int amount = pointsAmount.getAmount();
        int pa = pri*points/amount;
        PointsUserHistory pointsUserHistory  = new PointsUserHistory();
        pointsUserHistory.setUsePoints(pa);
        if (parentId == null) {
            parentId = sysChildMapper.selectSysChildById(childId).getParentId();
        }
        pointsUserHistory.setUserId(parentId);
        pointsUserHistory.setUserTime(DateUtils.getNowDate());
        pointsUserHistoryService.insertPointsUserHistory(pointsUserHistory);
        // 添加孩子课程详情
        List<CoursePeriod> periodList = coursePeriodMapper.selectCoursePeriodByInfoId(infoId);
        for (CoursePeriod coursePeriod : periodList) {
            CoursePeriodChild coursePeriodChild = new CoursePeriodChild();
            coursePeriodChild.setPeriodId(coursePeriod.getId());
            coursePeriodChild.setChildId(childId);
            coursePeriodChild.setCreateBy(ShiroUtils.getLoginName());
            coursePeriodChild.setCreateTime(DateUtils.getNowDate());
            coursePeriodChild.setDelFlag("0");
            coursePeriodChildMapper.insertCoursePeriodChild(coursePeriodChild);
        }
        // 添加孩子课程
        courseInfoChild.setCreateBy(ShiroUtils.getLoginName());
        courseInfoChild.setCreateTime(DateUtils.getNowDate());
        courseInfoChild.setDelFlag("0");
        return courseInfoChildMapper.insertCourseInfoChild(courseInfoChild);
    }

    /**
     * 修改课程学生
     * 
     * @param courseInfoChild 课程学生
     * @return 结果
     */
    @Override
    public int updateCourseInfoChild(CourseInfoChild courseInfoChild)
    {
        courseInfoChild.setUpdateTime(DateUtils.getNowDate());
        courseInfoChild.setUpdateBy(ShiroUtils.getLoginName());
        return courseInfoChildMapper.updateCourseInfoChild(courseInfoChild);
    }

    /**
     * 批量删除课程学生
     * 
     * @param ids 需要删除的课程学生主键
     * @return 结果
     */
    @Override
    public int deleteCourseInfoChildByIds(String ids)
    {
        return courseInfoChildMapper.deleteCourseInfoChildByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程学生信息
     * 
     * @param id 课程学生主键
     * @return 结果
     */
    @Override
    public int deleteCourseInfoChildById(Long id)
    {
        return courseInfoChildMapper.deleteCourseInfoChildById(id);
    }
}
